#include <stdio.h>
#include <stdint.h>
#include <openssl/aes.h>
#include <string.h>

void printAll(char* start, int length)
{
    for(int i=0;i<length;i++){
        printf("%d",start[i]);
    }
    printf("\n");
}

void aesCBCencrypt()
{
    //对称加密
    const char * str="hello Alice, my name is daniel. do you have a boyfriend? if not, maybe i can be your boyfriend.";
    //准备必要
    const char* key="1236547887654123";
    printf("before encrypt:");
    printAll(str,strlen(str));
    //初始化加密密钥
    AES_KEY aesKey;
    int ret=AES_set_encrypt_key((uint8_t*) key,128,&aesKey);
    //加密
    int len=strlen(str)+1;
    len=(len%16==0?len:(len/16+1)*16);
    char buf[len];
    memset((void*)buf,0,sizeof(buf));
    char decryptBuf[len];
    memset((void*)decryptBuf,0,sizeof(decryptBuf));
    uint8_t ivec[AES_BLOCK_SIZE];
    memset((void*)ivec,46,AES_BLOCK_SIZE);
    AES_cbc_encrypt((uint8_t*)str,(uint8_t*)buf,len,&aesKey,ivec,AES_ENCRYPT);
    printf("after encrypt:");
    printAll(buf,len);
    printf("\n");
    ret=AES_set_decrypt_key((uint8_t*)key,128,&aesKey);
    memset((void*)ivec,46,AES_BLOCK_SIZE);
    AES_cbc_encrypt((uint8_t*)buf,(uint8_t*)decryptBuf,len,&aesKey,ivec,AES_DECRYPT);
    printf("after decrypt:");
    printAll(decryptBuf,len);
    printf("\n");
}

void testMyAes()
{

}

int main()
{
    aesCBCencrypt();
    return 0;
}